package com.stx.crazyblog.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stx.crazyblog.dao.CommentDao;
import com.stx.crazyblog.entity.Comment;
/*
 * 添加评论内容的servlet方法
 * */
public class CommentAddServlet  extends HttpServlet{
//	保持版本的兼容性，保证在版本升级的时候反序列仍能保证对象的唯一性
	private static final long serialVersionUID = 1l;
//	初始化评论dao对象
	CommentDao dao = null;
//	将初始化dao对象的方法放入init()中去，并且保证dao只能被创造一次
	public  void init(){
//	初始化dao对象
		dao =new CommentDao();
		}
/**
 * @see HttpServlet#doGet(HttpServletRequest request,
 *				HttpServletResponse response)
 * 
 * */		
		protected void doGet(HttpServletRequest request,
				HttpServletResponse response)
						throws ServletException,IOException{
			doPost(request,response);
		}
/**
 * @see HttpServlet#doPost(HttpServletRequest request,
 *						HttpServletResponse response)
 * 
 * */		
		protected void doPost(HttpServletRequest request,
				HttpServletResponse response)
						throws ServletException,IOException{
//	设定请求编码集为uft-8 防止中文出现乱码		
			request.setCharacterEncoding("utf-8");
//	创建评论类数据视图对象（用于封装评论信息查询到的内容）		
			Comment commentParams = new Comment();
//	从页面获取新增数据
			commentParams.setContent(request.getParameter("co_content"));

		try{
//			调用dao中的插入评论数据的方法
			dao.insertComment(commentParams);
//			将结果保存到request中
			request.setAttribute("message", "评论成功");
//			调用servlet中的查询方法，更新评论信息表
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/CommentAddServlet");
//			将请求转发给servlet
			rd.forward(request, response);
		}catch(Exception e){
//			将结果保存到request中
			request.setAttribute("message", "评论成功");
//			调用servlet中的查询方法，更新评论信息表
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/CommentAddServlet");
//			将请求转发给servlet
			rd.forward(request, response);
		}
	}
	
	
}
